﻿/// /////////////////////////////////////////////
// 单元名 : BaseDialog
// 作者 : Hawk
// 单元描述 :
// 日期 : 2023-09-15
/// /////////////////////////////////////////////

{$IFDEF FPC}
{$H+}
{$MODE DELPHI}
{$ENDIF}
unit BaseDialog;

interface

uses
{$IFnDEF FPC}
  Windows,
{$ELSE}
  LCLIntf, LCLType, LMessages,
{$ENDIF}
  Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons,
  unitCommonDataStruct, unitDataCenter;

type
  TdlgBase = class;
  TdlgBaseClass = class of TdlgBase;

  TdlgBase = class(TForm)
    btnOK: TBitBtn;
    btnCancel: TBitBtn;
    Bevel1: TBevel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FObj: TSerialObj;
    FDataCenterSet: TDataCenterSet;
    FPeerHost: TID_HOST;
    FLocalID: TID_HOST;
    procedure SetObj(const Value: TSerialObj);
    { Private declarations }
  protected
    procedure ShowErrorMsg(msg: string; Param: array of const);
    procedure ShowHintMsg(msg: string; Param: array of const);
    function BindObjCls: TSerialObjClass; virtual;
    procedure DoObjToUI; virtual;
    procedure DoUIToObj; virtual;
  public
    { Public declarations }
    class function ShowInfoDlg(const ph, lh: TID_HOST; dcs: TDataCenterSet; initObj: TSerialObj; config: TSerialObj)
      : TSerialObj;

    procedure SetConfig(config: TSerialObj); virtual;

    property DataCenterSet: TDataCenterSet read FDataCenterSet write FDataCenterSet;
    property LocalID: TID_HOST read FLocalID write FLocalID;
    property PeerHost: TID_HOST read FPeerHost write FPeerHost;
    property Obj: TSerialObj read FObj write SetObj;
  end;

  // function ShowInfoDlg(dlgcls: TdlgBaseClass; initObj: TSerialObj; config: TSerialObj): TSerialObj;

implementation

uses unitObjPool;
{$IFnDEF FPC}
  {$R *.dfm}
{$ELSE}
  {$R *.lfm}
{$ENDIF}
{ TdlgBase }

function TdlgBase.BindObjCls: TSerialObjClass;
begin
  Result := nil;
end;

procedure TdlgBase.DoObjToUI;
begin

end;

procedure TdlgBase.DoUIToObj;
begin

end;

procedure TdlgBase.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  DoUIToObj;
end;

procedure TdlgBase.SetConfig(config: TSerialObj);
begin

end;

procedure TdlgBase.SetObj(const Value: TSerialObj);
begin
  FObj := Value;
  if Value <> nil then
    DoObjToUI;
end;

procedure TdlgBase.ShowErrorMsg(msg: string; Param: array of const);
begin
  msg := Format(msg, Param);
  MessageBox(Handle, PChar(msg), '错误', MB_OK or MB_ICONSTOP);
end;

procedure TdlgBase.ShowHintMsg(msg: string; Param: array of const);
begin
  msg := Format(msg, Param);
  MessageBox(Handle, PChar(msg), '信息', MB_OK or MB_ICONINFORMATION);
end;

class function TdlgBase.ShowInfoDlg(const ph, lh: TID_HOST; dcs: TDataCenterSet; initObj, config: TSerialObj)
  : TSerialObj;
var
  f: TdlgBase;
begin
  f := Self.Create(Application);
  f.FDataCenterSet := dcs;
  f.FPeerHost := ph;
  f.FLocalID := lh;
  f.SetConfig(config);
  if initObj <> nil then
    Result := initObj.Clone
  else
    Result := ObjPool.GetObj(f.BindObjCls, nil);
  f.Obj := Result;
  if f.ShowModal <> mrOK then
  begin
    ObjPool.ReturnObj(Result, nil);
    Result := nil;
  end;
  f.Free;
end;

end.
